package com.xc.hotelmgr.webapi.modules.op.service.impl;

import com.google.common.collect.Lists;
import com.xc.hotelmgr.webapi.common.utils.R;
import com.xc.hotelmgr.webapi.common.utils.RedisUtils;
import com.xc.hotelmgr.webapi.modules.dt.entity.HtlEntity;
import com.xc.hotelmgr.webapi.modules.op.entity.UpHtlDto;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.hotelmgr.webapi.common.utils.PageUtils;
import com.xc.hotelmgr.webapi.common.utils.Query;

import com.xc.hotelmgr.webapi.modules.op.dao.DtUpHtlDao;
import com.xc.hotelmgr.webapi.modules.op.entity.DtUpHtlEntity;
import com.xc.hotelmgr.webapi.modules.op.service.DtUpHtlService;


@Service("dtUpHtlService")
@RequiredArgsConstructor
public class DtUpHtlServiceImpl extends ServiceImpl<DtUpHtlDao, DtUpHtlEntity> implements DtUpHtlService {

    private final RedisUtils redisUtils;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DtUpHtlEntity> page = this.page(
                new Query<DtUpHtlEntity>().getPage(params),
                new QueryWrapper<DtUpHtlEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R prePareSave(UpHtlDto upHtlDto) {
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("dtId",upHtlDto.getDtId());
        params.put("storId",upHtlDto.getStorId());
        params.put("province",upHtlDto.getProvince());
        params.put("city",upHtlDto.getCity());
        params.put("county",upHtlDto.getCounty());
        params.put("brand",upHtlDto.getBrand());
        List<DtUpHtlEntity> upHtlEntities = null;
        R r = null;
        //批量上架所有酒店
        if(upHtlDto.getAuthType().equals("1") && upHtlDto.getAuthAll().equals("1")) {
            upHtlEntities = Lists.newArrayList();
            DtUpHtlEntity dtUpHtlEntity = null;
            Long id = null;
            List<HtlEntity> hotels = baseMapper.sqlQueryHtl(params);
            if(hotels.size() <= 0){
                r = R.ok("所有酒店都已经上架，无需要上架的酒店");
            }else {
                r = R.ok("分配了【"+ hotels.size() +"】个酒店都店铺，分配酒店需要一段时间，请耐心等待。");
            }
            for (HtlEntity h : hotels){
                dtUpHtlEntity = new DtUpHtlEntity();
                dtUpHtlEntity.setDtId(Integer.valueOf(upHtlDto.getDtId()));
                dtUpHtlEntity.setStoreId(Integer.valueOf(upHtlDto.getStorId()));
                dtUpHtlEntity.setHotelId(h.getHotelCode());
                dtUpHtlEntity.setStatus(-1);
                id = Long.valueOf(h.getHotelCode().toString() + upHtlDto.getStorId());
                dtUpHtlEntity.setId(id);
                upHtlEntities.add(dtUpHtlEntity);
            }
        }
        //上架足指定条件酒店
        if(upHtlDto.getAuthType().equals("1") && upHtlDto.getAuthAll().equals("0")){
            upHtlEntities = Lists.newArrayList();
            DtUpHtlEntity dtUpHtlEntity = null;
            Long id = null;
            List<HtlEntity> hotels = baseMapper.sqlQueryHtl(params);
            if(hotels.size() <= 0){
                r = R.ok("所有满足酒店都已经上架，无需要上架的酒店");
            }else {
                r = R.ok("分配了【"+ hotels.size() +"】个酒店到店铺，分配需要一段时间，请耐心等待。");
            }
            for (HtlEntity h : hotels){
                dtUpHtlEntity = new DtUpHtlEntity();
                dtUpHtlEntity.setDtId(Integer.valueOf(upHtlDto.getDtId()));
                dtUpHtlEntity.setStoreId(Integer.valueOf(upHtlDto.getStorId()));
                dtUpHtlEntity.setHotelId(h.getHotelCode());
                dtUpHtlEntity.setStatus(-1);
                id=Long.valueOf(h.getHotelCode().toString() + upHtlDto.getStorId());
                dtUpHtlEntity.setId(id);
                upHtlEntities.add(dtUpHtlEntity);
            }
        }
        //分配指定酒店
        if(upHtlDto.getAuthType().equals("2") || upHtlDto.getAuthType().equals("3")){
            upHtlEntities = Lists.newArrayList();
            DtUpHtlEntity dtUpHtlEntity = null;
            Long id = null;
            if(upHtlDto.getHotelIds().length <= 0){
                r = R.ok("请选择需要分配的酒店");
            }else {
                r = R.ok("分配了【"+upHtlDto.getHotelIds().length +"】个酒店到店铺，分配需要一段时间，请耐心等待。");
            }
            for (String hid : upHtlDto.getHotelIds()){
                dtUpHtlEntity = new DtUpHtlEntity();
                dtUpHtlEntity.setDtId(Integer.valueOf(upHtlDto.getDtId()));
                dtUpHtlEntity.setStoreId(Integer.valueOf(upHtlDto.getStorId()));
                dtUpHtlEntity.setHotelId(Long.valueOf(hid));
                dtUpHtlEntity.setStatus(-1);
                //id = Long.valueOf(upHtlDto.getStorId() + hid);
                id = Long.valueOf(hid + upHtlDto.getStorId());
                dtUpHtlEntity.setId(id);
                upHtlEntities.add(dtUpHtlEntity);
            }
        }
        r.put("upHtl",upHtlEntities);
        return  r;
    }
    @Async
    public void saveUphtl(List<DtUpHtlEntity> upHtlEntities){
//        List<Long> listHotel = upHtlEntities.stream().map(DtUpHtlEntity::getHotelId).collect(Collectors.toList());
//        Long[] hotelIds = listHotel.toArray(new Long[listHotel.size()]);
//        redisUtils.sSet("goods_id_list",hotelIds);
        saveOrUpdateBatch(upHtlEntities);
    }

    @Override
    public PageUtils sqlQueryPage(Map<String, Object> params) {
        IPage<DtUpHtlEntity> page = new Query<DtUpHtlEntity>().getPage(params);
        params.put("page",page);
        List<DtUpHtlEntity> list = baseMapper.sqlQueryPage(params);
        PageUtils pageUtils = new PageUtils(page,list);
        return pageUtils;
    }
    @Override
    public PageUtils sqlQueryPageByName(Map<String, Object> params) {
        IPage<DtUpHtlEntity> page = new Query<DtUpHtlEntity>().getPage(params);
        params.put("page",page);
        List<DtUpHtlEntity> list = baseMapper.sqlQuery(params);
        PageUtils pageUtils = new PageUtils(page,list);
        return pageUtils;
    }
    //查询指定店铺下的酒店，用于分组使用
    @Override
    public List<DtUpHtlEntity> sqlQueryByStore(Map<String, Object> params) {
        List<DtUpHtlEntity> list = baseMapper.sqlQueryByStore(params);
        return list;
    }

    @Override
    public PageUtils sqlQueryHtlPage(Map<String, Object> params) {
        IPage<HtlEntity> page = new  Query<HtlEntity>().getPage(params);
     params.put("page",page);
        List<HtlEntity> list = baseMapper.sqlQueryHtlPage(params);
        return new  PageUtils(page,list);
    }

    @Override
    public void redisInit() {
        List<Long> listRP = baseMapper.sqlQueryUpHotelRp();
        Long[] rpCodes = listRP.toArray(new Long[listRP.size()]);
//        List<DtUpHtlEntity> upHtlEntities = baseMapper.selectList(new QueryWrapper<DtUpHtlEntity>().select("DISTINCT hotel_id"));
//        List<Long> listHotel = upHtlEntities.stream().map(DtUpHtlEntity::getHotelId).collect(Collectors.toList());
//        Long[] hotelIds = listHotel.toArray(new Long[listHotel.size()]);
        redisUtils.delete("goods_id_list");
        redisUtils.sSet("goods_id_list", rpCodes);
    }

    @Override
    public List<Long> getHotelList(Integer status) {
        return baseMapper.getHotelList(status);
    }

    @Override
    public List<Long> getByStoreId(int storeId) {

        return baseMapper.getByStoreId(storeId);
    }

    @Override
    public List<Long> getByAreaId(Integer storeId, Long sendId) {
        return baseMapper.getByAreaId(storeId,sendId);
    }

    @Override
    public List<Long> getByGroupId(Integer storeId, Long groupId) {
        return baseMapper.getByGroupId(storeId,groupId);
    }
}